﻿using System;
using System.IO;
using System.Text;
using System.Xml.Serialization;
using Top.Api.Domain;

namespace TaobaoAPIDAO.App_Code
{

    public static class XmlHelper
    {
        private static User m_user = null;

        #region public static User GetObjFromXmlByFile(string path) 将文件对象的XML文件反序列化到对象
        /// <summary>
        /// 将文件对象的XML文件反序列化到对象
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static User GetObjFromXmlByFile(string path)
        {
            if (m_user == null)
            {
                FileStream fs = null;
                try
                {
                    XmlSerializer xs = new XmlSerializer(typeof(User));
                    fs = new FileStream(path, FileMode.Open, FileAccess.Read);
                    m_user = (User)xs.Deserialize(fs);
                    fs.Close();
                    return m_user;
                }
                catch
                {
                    if (fs != null)
                        fs.Close();
                    throw new Exception("Xml deserialization failed!");
                }

            }
            else
            {
                return m_user;
            }
        }

        #endregion

        #region public static User LoadObjFromXML(string data) 字符串的XML文件反序列化到对象
        /// <summary>
        /// 字符串的XML文件反序列化到对象
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static User LoadObjFromXMLForUser(string data)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                using (StreamWriter sw = new StreamWriter(stream, Encoding.UTF8))
                {
                    sw.Write(data);
                    sw.Flush();
                    stream.Seek(0, SeekOrigin.Begin);
                    XmlSerializer serializer = new XmlSerializer(typeof(User));
                    try
                    {
                        return ((User)serializer.Deserialize(stream));
                    }
                    catch
                    {
                        return default(User);
                    }

                }
            }
        }
        #endregion

        #region public static Shop LoadObjFromXMLForShop(string data) 字符串的XML文件反序列化到对象
        /// <summary>
        /// 字符串的XML文件反序列化到对象
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static Shop LoadObjFromXMLForShop(string data)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                using (StreamWriter sw = new StreamWriter(stream, Encoding.UTF8))
                {
                    sw.Write(data);
                    sw.Flush();
                    stream.Seek(0, SeekOrigin.Begin);
                    XmlSerializer serializer = new XmlSerializer(typeof(Shop));
                    try
                    {
                        return ((Shop)serializer.Deserialize(stream));
                    }
                    catch
                    {
                        return default(Shop);
                    }

                }
            }
        }
        #endregion

        #region public static Trade LoadObjFromXML(string data) 字符串的XML文件反序列化到对象
        /// <summary>
        /// 字符串的XML文件反序列化到对象
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static Trade LoadObjFromXMLForTrade(string data)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                using (StreamWriter sw = new StreamWriter(stream, Encoding.UTF8))
                {
                    sw.Write(data);
                    sw.Flush();
                    stream.Seek(0, SeekOrigin.Begin);
                    XmlSerializer serializer = new XmlSerializer(typeof(User));
                    try
                    {
                        return ((Trade)serializer.Deserialize(stream));
                    }
                    catch
                    {
                        return default(Trade);
                    }

                }
            }
        }
        #endregion
    }
}
